package com.yc.algorithm;

import jxl.demo.Demo;
import lombok.Data;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Data
public class DemoOne {

    private Integer snode;
    private Integer enode;

    public DemoOne(Integer snode, Integer enode) {
        this.snode = snode;
        this.enode = enode;
    }

    @Override
    public String toString() {
        return "DemoOne{" +
                "snode=" + snode +
                ", enode=" + enode +
                '}';
    }

    public static void main(String[] args) {
//        List<DemoOne> demoOne = createDemoOne();
//        for (DemoOne one : demoOne) {
//            System.out.println(one);
//        }

        LinkedList<DemoOne> demos = getDemo();
        for (DemoOne demo : demos) {
            System.out.println(demo);
        }
        int size = demos.size();
        int index = size / 2;
        System.out.println(index);
        System.out.println(demos.get(index));
    }

    private static LinkedList<DemoOne> getDemo() {
        LinkedList<DemoOne> linkedList = new LinkedList<>();
        List<DemoOne> demoOnes = createDemoOne();
        boolean flag1 = true;
        boolean flag2 = true;
        DemoOne demoOne1 = demoOnes.get(0);
        linkedList.add(demoOne1);
        while (flag1) {
            for (DemoOne one : demoOnes) {
                if (demoOne1.getSnode() == one.getEnode()) {
                    linkedList.offerFirst(one);
                    demoOne1 = one;
                    flag1 = true;
                    break;
                } else {
                    flag1 = false;
                }
            }
        }
        DemoOne demoOne2 = linkedList.getLast();
        while (flag2) {
            for (DemoOne one : demoOnes) {
                if (demoOne2.getEnode() == one.getSnode()) {
                    linkedList.offer(one);
                    demoOne2 = one;
                    flag2 = true;
                    break;
                } else {
                    flag2 = false;
                }
            }
        }
        return linkedList;
    }


    private static List<DemoOne> createDemoOne() {
        List<DemoOne> list = new ArrayList(){{
            add(new DemoOne(2, 3));
            add(new DemoOne(1, 2));
            add(new DemoOne(4, 5));
//            add(new DemoOne(5, 6));
            add(new DemoOne(3, 4));
            add(new DemoOne(0, 1));
            add(new DemoOne(6, 7));
        }};
        return list;
    }

}
